package com.azusa.ktc_search.controller;

import com.azusa.ktc_search.entity.Article;
import com.azusa.ktc_search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description 文章 控制器层
 * @author Azusa
 * @date 2020-10-12 18:16:12
*/
@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
    * 增加
    * @param article
    */
    @RequestMapping(method=RequestMethod.POST)
    public Result add(@RequestBody  Article  article  ){
        articleService.add( article);
        return new Result(StatusCode.SUCCESS);
    }


    @RequestMapping(value = "/search/{keywords}/{page}/{size}", method = RequestMethod.GET)
    public Result search(@PathVariable String keywords, @PathVariable int page, @PathVariable int size) {
        Page<Article> pageData=articleService.search(keywords,page,size);
        return new Result(StatusCode.SUCCESS,new PageResult<>(pageData.getTotalElements(),pageData.getContent()));

    }

}
